<template>
    <van-nav-bar title="系列管理" left-text="返回" left-arrow @click-left="$router.back()"></van-nav-bar>
    <div class="content">
        <div class="title">
            <span>{{ $route.query.name }}</span>
        </div>
        <div class="card-list" ref="cardListRef">
            <div v-for="(item, index) in cardList" :key="item.id" class="card-item">
                <div class="card-content">
                    <div class="card-info">
                        <div class="card-name">{{ item.name }}</div>
                    </div>
                </div>
                <div style="display: flex; align-items: center;">
                    <div class="edit-button" @click="handleEdit(item)">
                        <van-icon name="edit" size="20"></van-icon>
                    </div>
                    <div class="drag-handle">
                        <van-icon name="bars" size="20" />
                    </div>  
                </div>
            </div>
        </div>
        
        <van-empty v-if="cardList.length === 0" description="暂无数据" />
        <div class="add-btn">
            <van-button type="primary" icon="plus" round block @click="addNewCard">添加系列</van-button>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { showToast } from 'vant';
import Sortable from 'sortablejs';
import { http } from '../../utils/request';

const router = useRouter();
const route = useRoute();
const cardListRef = ref(null);
const cardList = ref([]);

const getList = () => {
    http.get('/category/getSecondLevelCategory?parentId=' + route.query.id).then(res => {
        cardList.value = res || [];
    });
}

// 初始化拖拽排序
onMounted(() => {
    if (cardListRef.value) {
        Sortable.create(cardListRef.value, {
            animation: 150,
            handle: '.drag-handle',
            onEnd: function(evt) {
                const itemEl = cardList.value.splice(evt.oldIndex, 1)[0];
                cardList.value.splice(evt.newIndex, 0, itemEl);
                updateOrder();
            }
        });
    }

    getList();
});

// 更新排序
const updateOrder = async () => {
    try {
        // 这里应该调用API更新排序
        const orderData = cardList.value.map((item, index) => ({
            id: item.id,
            sort: index
        }));
        console.log('更新排序:', orderData);
        
        // 模拟API调用
        await http.post('/category/saveSorts', orderData)
        showToast('排序更新成功');
    } catch (error) {
        console.error('排序更新失败:', error);
        showToast('排序更新失败');
    }
};

// 编辑卡片
const handleEdit = (item) => {
    router.push('/category/second/edit?id=' + item.id + '&parentId=' + route.query.id);
};

// 添加新卡片
const addNewCard = () => {
    router.push('/category/second/add?parentId=' + route.query.id);
};
</script>

<style lang="scss" scoped>
.content {
    background-color: #f7f8fa;
    min-height: calc(100vh - 60px);
    .title {
        display: flex;
        width: calc(100% - 40px);
        border-top: 1px solid #ababab;
        margin: 10px 20px;

        span {
            position: relative;
            display: inline-block;
            left: 50%;
            transform: translateX(-50%);
            top: -8px;
            background: #f7f8fa;
            font-size: 12px;
            padding: 0 10px;
        }
    }
    
    .card-list {
        padding: 0 16px;
    }
    
    .card-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 12px;
        padding: 12px;
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    }
    
    .card-content {
        display: flex;
        align-items: center;
        flex: 1;
    }
    
    .card-image {
        width: 60px;
        height: 60px;
        border-radius: 4px;
        object-fit: cover;
        margin-right: 12px;
    }
    
    .card-info {
        flex: 1;
    }
    
    .card-name {
        font-size: 16px;
        font-weight: 500;
        margin-bottom: 4px;
    }
    
    .card-desc {
        font-size: 12px;
        color: #999;
    }

    .edit-button {
        padding: 8px;
        color: #999;
        cursor: pointer;
    }
    
    .drag-handle {
        padding: 8px;
        color: #999;
        cursor: move;
        touch-action: none;
    }
    
    .add-btn {
        position: fixed;
        bottom: 20px;
        left: 16px;
        right: 16px;
    }
}
</style>